$(function () {
    let list = {
        url: {
            getList2: 'front/list'
        },
        init: function () {
            let _self = list
            // 列表匹配顶部菜单
            let thisUrl = window.location.href
            let hasNav = false
            $('.nav a').each(function() {
                if (thisUrl.indexOf($(this).attr('href')) !== -1) {
                    hasNav = true
                    $(this).addClass('now').siblings().removeClass('now')
                }
            })
            if (hasNav === false) {
                $('.nav a').removeClass('now')
            }
            // 右侧列表（分页）
            _self.toList2(1)

            $('#listMainPage').on('click', 'a', (event) => {
                _self.toList2($(event.target).attr('page'))
            })
        },
        toList2: (curpage, size = 10) => {
            let _self = list
            let param = {
                ckType: getUrlParam('ckType') || '',
                inputVal: decodeURI(getUrlParam('inputVal')) || '',
                curpage: curpage,
                size: size
            }
            $.post(list.url.getList2, param, (data) => {
                if (data.success) {
                    let res = data.result
                    if (res.length === 0) {
                        $('#listMain').html('<div class="alcenter pt10 pb10">暂无数据</div>')
                    } else {
                        let arr = []
                        curpage = parseInt(curpage, 10)
                        res.map((value, index) => {
                            arr.push('<li><a href="' + value.link + '"><span class="list4_l">' + value.title + '</span><span class="list4_r">' + value.createdate + '</span></a></li>')
                        })
                        $('#listMain').html(arr.join(''))

                        let pageArr = []
                        let sum = Math.ceil(data.total / size)
                        pageArr.push('<a href="javascript:;" page="1">首页</a>')
                        pageArr.push('<a href="javascript:;" page="' + (curpage - 1 === 0 ? "1" : curpage - 1) + '">上一页</a>')
                        if (sum < 10) {
                            for (let i = 1; i <= sum; i++) {
                                pageArr.push('<a href="javascript:;" page="' + i + '" class="' + (i === curpage ? "now" : "") + '" >' + i + '</a>')
                            }
                        } else {
                            if (curpage < 10) {
                                for (let i = 1; i < 10; i++) {
                                    pageArr.push('<a href="javascript:;" page="' + i + '" class="' + (i === curpage ? "now" : "") + '">' + i + '</a>')
                                }
                                pageArr.push('<span>...</span>')
                            } else if (curpage > (sum - 9)) {
                                pageArr.push('<span>...</span>')
                                for (let i = sum - 8; i <= sum; i++) {
                                    pageArr.push('<a href="javascript:;" page="' + i + '" class="' + (i === curpage ? "now" : "") + '">' + i + '</a>')
                                }
                            } else {
                                pageArr.push('<span>...</span>')
                                for (let i = curpage - 4; i <= curpage + 4; i++) {
                                    pageArr.push('<a href="javascript:;" page="' + i + '" class="' + (i === curpage ? "now" : "") + '">' + i + '</a>')
                                }
                                pageArr.push('<span>...</span>')
                            }

                        }
                        pageArr.push('<a href="javascript:;" page="' + (curpage + 1 > sum ? sum : curpage + 1) + '">下一页</a>')
                        pageArr.push('<a href="javascript:;" page="' + sum + '">尾页</a>')
                        pageArr.push('<span>共' + data.total + '条</span><span>' + size + '条/页</span>')
                        $('#listMainPage').html(pageArr.join(''))
                    }

                }
            })
        }
    }
    list.init()
})